package com.tvein.sanguo.action.controller.system
{
	//flash
	//pureMVC
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.command.SimpleFabricationCommand;
	//custom
	import com.tvein.sanguo.action.common.GameConstants;
	import com.tvein.sanguo.action.view.ActionMediator;
	import com.tvein.sanguo.action.model.SystemProxy;
	import com.tvein.sanguo.swc.action.login.LoginGame;
	import com.tvein.sanguo.swc.action.role.RoleList;
//
	public class ActionScript extends SimpleFabricationCommand implements ICommand
	{
		private var actionMediator:ActionMediator;
		private var systemData:SystemProxy;
		
		override public function execute(notification:INotification) : void
		{
			actionMediator = facade.retrieveMediator(ActionMediator.NAME) as ActionMediator;
			systemData = facade.retrieveProxy(SystemProxy.NAME) as SystemProxy;
			
			var params:Object = new Object();
			
			switch(notification.getBody().Main.type)
			{
				case "Skill":
					clearChild();
					sendNotification(GameConstants.ACTION_SKILL, notification.getBody());
					break;
				case "Command":
					clearChild();
					sendNotification(GameConstants.ACTION_COMMAND, notification.getBody());
					break;
				case "Room":
					clearChild();
					sendNotification(GameConstants.ACTION_ROOM, notification.getBody());
					break;
				case "SQLData":
					var i:int = actionMediator.action.numChildren;
					
					if (i > 0)
					{
						if (actionMediator.action.getChildAt(i - 1).hasOwnProperty("console"))
						{
							actionMediator.action.getChildAt(i - 1)["console"]("setData", notification.getBody());
						}
					}
					
					break;
				case "GameInfo":
					clearChild();
					
					var loginGame:LoginGame = new LoginGame();
					actionMediator.action.addChild(loginGame);
					loginGame.name = "Login-Game";
					
					params.Volume = systemData.effectVol;
					loginGame.console("setEffect", params);
					
					loginGame.console("setGameInfo", notification.getBody().Process);					
					loginGame.addEventListener("Login-Game", actionMediator._handleEvents);
					
					break;
				case "RoleList":
					clearChild();
					routeNotification(GameConstants.MODULE_INVOKE, ["-init", ["show", false]], null, "*");
					
					var roleList:RoleList = new RoleList();
					actionMediator.action.addChild(roleList);					
					roleList.name = "Role-List";
					
					params.Address = systemData.srcServer;
					roleList.console("setSource", params);
					params.Volume = systemData.effectVol;
					roleList.console("setEffect", params);
					
					roleList.console("setRoleList", notification.getBody());					
					roleList.addEventListener("Role-Choice", actionMediator._handleEvents);
					
					break;
				case "GameSound":
					for (var s:int = 0; s < notification.getBody().Process.length; s++)
					{
						switch(notification.getBody().Process[s].Config_ID)
						{
							case 1:
								routeNotification(GameConstants.MODULE_INVOKE, ["-init", ["musicVol", int(notification.getBody().Process[s].Value)]], null, "*");
								sendNotification(GameConstants.ACTION_INIT, ["musicVol", int(notification.getBody().Process[s].Value)]);
								break;
							case 2:
								routeNotification(GameConstants.MODULE_INVOKE, ["-init", ["effectVol", int(notification.getBody().Process[s].Value)]], null, "*");
								sendNotification(GameConstants.ACTION_INIT, ["effectVol", int(notification.getBody().Process[s].Value)]);
								break;
							case 3:
								routeNotification(GameConstants.MODULE_INVOKE, ["-init", ["voiceVol", int(notification.getBody().Process[s].Value)]], null, "*");
								sendNotification(GameConstants.ACTION_INIT, ["voiceVol", int(notification.getBody().Process[s].Value)]);
								break;
						}
					}
					
					break;
			}
		}
		
		private function clearChild():void
		{
			for (var i:int = actionMediator.action.numChildren - 1; i >= 0; i--)
			{
				actionMediator.action.removeChildAt(0); 
			}
		}
	}
}